import * as types from '../action-typess';
export default function reducer(state={
  page : 1,
  list: [],
  fetched: false,
  detail: {
    data: [{
      user: {}
    }]
  },
  applyJob : {
    page : 1,
    list : []
  },
  token: {}
}, action) {
  //判断类型做出对应state的改变
  switch (action.type){
    // 社区帖子列表
    case types.FETCH_TOPICLIST: {
      return {...state, fetching: true}
    }
    case types.FETCH_TOPICLIST_FULFILLED: {
      if(action.payload.data){
        return {
          ...state,
          fetching: false,
          fetched: true,
          list: [...state.list,...action.payload.data.data],
          page: state.page + 1
        }
      }
    }
     // 社区帖子列表
     case types.FETCH_APPLY_JOB: {
      return {...state, fetching: true}
    }
    case types.FETCH_APPLY_JOB_FULFILLED: {
      if(action.payload.data){
        return {
          ...state,
          fetching: false,
          fetched: true,
          applyJob : {
            page : state.applyJob.page + 1,
            list : [...state.applyJob.list,...action.payload.data.data]
          }
        }
      }
    }
    // 发帖
    case types.SUBMIT_TOPIC: {
      return {...state, fetching: true, postTopRet : -1}
    }
    case types.SUBMIT_TOPIC_FULFILLED: {
      if(action.payload.data){
        return {
          ...state,
          fetching: false,
          fetched: true,
          postTopRet :  action.payload.ret,
          postTopMsg : action.payload.msg
        }
      }
    }
    // 帖子详情
    case types.TOPIC_DETAIL: {
      return {...state, fetching: true}
    }
    case types.TOPIC_DETAIL_FULFILLED: {
      if(action.payload.data){
        return {
          ...state,
          fetching: false,
          fetched: true,
          detail: action.payload.data
        }
      }
    }
    // oss
    case types.OSS_TOKEN: {
      return {...state, fetching: true}
    }
    case types.OSS_TOKEN_FULFILLED: {
      if(action.payload.data){
        return {
          ...state,
          fetching: false,
          fetched: true,
          token: action.payload.data
        }
      }
    }
    // 提交评论
    case types.SUBMIT_COMMENT: {
      return {...state, fetching: true}
    }
    case types.SUBMIT_COMMENT_FULFILLED: {
      if(action.payload.data){
        return {
          ...state,
          fetching: false,
          fetched: true
        }
      }
    }
    // 点赞
    case types.LIKE_COMMENT: {
      return {...state, fetching: true}
    }
    case types.LIKE_COMMENT_FULFILLED: {
      if(action.payload.data){
        return {
          ...state,
          fetching: false,
          fetched: true
        }
      }
    }
    // 传图
    case types.UPLOAD_IMG: {
      return {...state, fetching: true}
    }
    case types.UPLOAD_IMG_FULFILLED: {
      if(action.payload.data){
        return {
          ...state,
          fetching: false,
          fetched: true
        }
      }
    }
  }
  return state;
}